# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymongo
from itemadapter import ItemAdapter


class BlibliPipeline:
    def open_spider(self, spider):
        print("open spider")
        self.client = pymongo.MongoClient()
        self.db = self.client['blibli']
        self.collection = self.db['汽车']
        self.items = []
        self.count = 0

    def process_item(self, item, spider):
        self.collection.insert_one(item)
        self.count += 1
        # self.items.append(item)
        # if len(self.items) == 10:
        #     self.collection.insert_many(self.items)
        #     self.items.clear()
        return item

    def close_spider(self, spider):
        print("close spider")
        print(self.count)
        # if self.items:
        #     self.collection.insert_many(self.items)
        #     self.items.clear()
        self.client.close()
